<template>
  <el-container class="dashboard-container">
    <el-header>
      <div class="header-content">
        <h2>工作台</h2>
        <el-button @click="router.push('/home')">返回首页</el-button>
      </div>
    </el-header>
    <el-main>
      <el-row :gutter="20">
        <el-col :span="16">
          <el-card class="todo-card">
            <template #header>
              <div class="card-header">
                <h3>待办事项</h3>
                <el-button type="primary" @click="refreshTodoList">
                  <el-icon><Refresh /></el-icon>
                  刷新
                </el-button>
              </div>
            </template>
            <el-tabs v-model="activeTab">
              <el-tab-pane label="待审批" name="approvals">
                <el-table :data="approvals" style="width: 100%">
                  <el-table-column prop="title" label="标题" />
                  <el-table-column prop="type" label="类型" width="120" />
                  <el-table-column prop="submitter" label="提交人" width="120" />
                  <el-table-column prop="submitTime" label="提交时间" width="180" />
                  <el-table-column label="操作" width="200">
                    <template #default="{ row }">
                      <el-button type="primary" size="small" @click="handleApproval(row)">审批</el-button>
                      <el-button type="info" size="small" @click="viewDetail(row)">查看</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
              <el-tab-pane label="待办任务" name="tasks">
                <el-table :data="tasks" style="width: 100%">
                  <el-table-column prop="title" label="标题" />
                  <el-table-column prop="type" label="类型" width="120" />
                  <el-table-column prop="assignee" label="负责人" width="120" />
                  <el-table-column prop="deadline" label="截止时间" width="180" />
                  <el-table-column label="操作" width="200">
                    <template #default="{ row }">
                      <el-button type="primary" size="small" @click="handleTask(row)">处理</el-button>
                      <el-button type="info" size="small" @click="viewDetail(row)">查看</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            </el-tabs>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card class="metrics-card">
            <template #header>
              <div class="card-header">
                <h3>关键指标</h3>
              </div>
            </template>
            <div class="metrics-list">
              <div v-for="metric in metrics" :key="metric.id" class="metric-item">
                <div class="metric-header">
                  <span class="metric-title">{{ metric.title }}</span>
                  <el-tag :type="metric.trend === 'up' ? 'success' : 'danger'" size="small">{{ metric.trend === 'up' ? '↑' : '↓' }} {{ metric.change }}</el-tag>
                </div>
                <div class="metric-value">{{ metric.value }}</div>
                <div class="metric-description">{{ metric.description }}</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-main>
  </el-container>
</template>

<script lang="ts" setup>
  import { ref } from 'vue'
  import { useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'
  import { Refresh } from '@element-plus/icons-vue'

  interface TodoItem {
    id: number
    title: string
    type: string
    submitter?: string
    assignee?: string
    submitTime?: string
    deadline?: string
  }

  interface Metric {
    id: number
    title: string
    value: string
    change: string
    trend: 'up' | 'down'
    description: string
  }

  const router = useRouter()
  const activeTab = ref('approvals')

  const approvals = ref<TodoItem[]>([
    {
      id: 1,
      title: '项目立项申请',
      type: '立项审批',
      submitter: '张三',
      submitTime: '2024-02-20 10:00:00',
    },
    {
      id: 2,
      title: '设备采购申请',
      type: '采购审批',
      submitter: '李四',
      submitTime: '2024-02-20 09:30:00',
    },
  ])

  const tasks = ref<TodoItem[]>([
    {
      id: 1,
      title: '完成项目周报',
      type: '文档任务',
      assignee: '王五',
      deadline: '2024-02-21 18:00:00',
    },
    {
      id: 2,
      title: '系统测试用例编写',
      type: '开发任务',
      assignee: '赵六',
      deadline: '2024-02-22 18:00:00',
    },
  ])

  const metrics = ref<Metric[]>([
    {
      id: 1,
      title: '项目完成率',
      value: '85%',
      change: '5%',
      trend: 'up',
      description: '较上月提升',
    },
    {
      id: 2,
      title: '任务完成率',
      value: '92%',
      change: '3%',
      trend: 'up',
      description: '较上月提升',
    },
    {
      id: 3,
      title: '系统可用性',
      value: '99.9%',
      change: '0.1%',
      trend: 'up',
      description: '保持稳定',
    },
    {
      id: 4,
      title: '平均响应时间',
      value: '1.2s',
      change: '0.3s',
      trend: 'down',
      description: '较上月下降',
    },
  ])

  const handleApproval = (item: TodoItem) => {
    ElMessage.success(`正在处理审批：${item.title}`)
  }

  const handleTask = (item: TodoItem) => {
    ElMessage.success(`正在处理任务：${item.title}`)
  }

  const viewDetail = (item: TodoItem) => {
    ElMessage.info(`查看详情：${item.title}`)
  }

  const refreshTodoList = () => {
    ElMessage.success('刷新成功')
  }
</script>

<style lang="scss" scoped>
  .dashboard-container {
    height: 100vh;

    .header-content {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 100%;
    }

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .todo-card,
    .metrics-card {
      height: 100%;
    }

    .metrics-list {
      display: flex;
      flex-direction: column;
      gap: 20px;
    }

    .metric-item {
      padding: 15px;
      border-radius: 4px;
      background-color: #f5f7fa;
    }

    .metric-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 10px;
    }

    .metric-title {
      font-weight: bold;
      color: #303133;
    }

    .metric-value {
      font-size: 24px;
      font-weight: bold;
      color: #409eff;
      margin: 10px 0;
    }

    .metric-description {
      font-size: 12px;
      color: #909399;
    }
  }
</style>
